package com.mock.ems.config;

import com.jfinal.config.*;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.ViewType;
import com.jfinal.template.Engine;
import com.mock.ems.controller.*;
import com.mock.ems.controller.common.UploadController;
import com.mock.ems.controller.config.*;
import com.mock.ems.controller.cooperation.*;
import com.mock.ems.controller.distribution.DistributionAreaAgentStatController;
import com.mock.ems.controller.distribution.DistributionConfigController;
import com.mock.ems.controller.distribution.DistributionController;
import com.mock.ems.controller.distribution.DistributionStatController;
import com.mock.ems.controller.operation.DataTestController;
import com.mock.ems.controller.operation.InterfaceTestController;
import com.mock.ems.controller.zone.*;
import com.mock.ems.interceptor.EmsActionInterceptor;
import com.mock.ems.redis.RedisConfig;
import com.mock.ems.redis.RedisPoolManager;
import com.mock.ems.schedule.QgSchduler;
import com.mock.ems.schedule.SecKillSchduler;
import com.mock.ems.schedule.UserManagerRemoveSchduler;
import com.mock.ems.task.DistributionTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.Executors;

/**
 * Created by lichengming on 2017/6/8.
 */
public class EmsConfig extends JFinalConfig {


    private static final Logger logger = LoggerFactory.getLogger(EmsConfig.class);

    @Override
    public void configConstant(Constants constance) {
        constance.setDevMode(true);
        constance.setViewType(ViewType.JSP);
        PropKit.use("jdbc.properties");
    }

    @Override
    public void configEngine(Engine arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void configHandler(Handlers arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void afterJFinalStart() {
        super.afterJFinalStart();
        RedisConfig redisConfig = RedisConfig.build()
                .redisHost("39.108.211.235")
                .redisPort(6379)
                .password("hzredis$");
        RedisPoolManager.init(redisConfig);
        Executors.newSingleThreadExecutor().execute(new DistributionTask());
        QgSchduler.schedule();
        SecKillSchduler.schedule();
        UserManagerRemoveSchduler.schedule();
    }

    @Override
    public void configInterceptor(Interceptors arg0) {
        // TODO Auto-generated method stub
        arg0.addGlobalActionInterceptor(new EmsActionInterceptor());

    }

    @Override
    public void configPlugin(Plugins plugins) {
        // TODO Auto-generated method stub

        DruidPlugin dp = new DruidPlugin(PropKit.get("url"), PropKit.get("username"),
                PropKit.get("password"), PropKit.get("driverClassName"));
        dp.setMinIdle(4);
        dp.setInitialSize(1);
        dp.setMaxWait(2000L);
        dp.setMaxActive(8);
        plugins.add(dp);
        ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
        plugins.add(arp);
        logger.info("[druid] data connection pool initial successfully!");
    }

    @Override
    public void configRoute(Routes routes) {

        routes.add("/", HomeController.class);
        routes.add("/interface", InterfaceTestController.class);
        routes.add("/user", UserController.class);
        routes.add("/admin", AdminController.class);
        routes.add("/tip", TipController.class);
        routes.add("/distribution", DistributionController.class);
        routes.add("/distributionstat", DistributionStatController.class);
        routes.add("/distributionareaagent", DistributionAreaAgentStatController.class);
        routes.add("/areaagent", AreaAgentController.class);
        routes.add("/param",SystemParamController.class);
        routes.add("/resource", ResourceController.class);
        routes.add("/datatest", DataTestController.class);
        routes.add("/servicecenter", CooperationServiceCenterController.class);
        routes.add("/partner", CooperationPartnerController.class);
        routes.add("/distributionconfig", DistributionConfigController.class);
        routes.add("/homepage", HomePageConfigController.class);
        routes.add("/upload", UploadController.class);
        routes.add("/merchant", MerchantController.class);
        routes.add("/userauth", UserAuthController.class);
        routes.add("/withdraw", WithdrawController.class);
        routes.add("/rechargerecord", RechargeRecordController.class);
        routes.add("/fundsrecord", FundsRecordController.class);
        routes.add("/shop", ShopController.class);
        routes.add("/shoporder", ShopOrderController.class);
        routes.add("/chatgroup", ChatGroupController.class);
        routes.add("/feedback", FeedBackController.class);
        routes.add("/qg", QgController.class);
        routes.add("/selldirect", SellDirectController.class);
        routes.add("/explosiveitem", ExplosiveItemController.class);
        routes.add("/seckill", SecKillController.class);
        routes.add("/zone", ZoneController.class);
        routes.add("/suggest",SuggestPController.class);
        routes.add("/score",ScoreController.class);
        routes.add("/scoreconf", ScoreConfigController.class);
        routes.add("/cloudcollege", CloudCollegeController.class);
        routes.add("/train", TrainConfigController.class);
        routes.add("/union", UnionController.class);
        routes.add("/trend",TrendController.class);
        routes.add("/notice",NoticeController.class);
        routes.add("/mall",MallController.class);
        routes.add("/scoredetail",ScoreDetailController.class);
        routes.add("/mallitem",MallItemController.class);
        routes.add("/trainorder",TrainOrderController.class);
        routes.add("/shopsort",ShopSortController.class);
        routes.add("/itemsort",ItemSortController.class);
        routes.add("/grouppurchase",GroupPurchaseController.class);
    }
}
